<?php 
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	
	//负责每日自动结算的自动程序
	//中文编码
	
	class auto extends  base{
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			set_time_limit(0);
			parent::__construct();
		}
		 
		/* public function auto_deal(){
        $i=0;
        do {
            echo $i.',';
            //周期订单禁止手动解约
            $this->run();//处理周期订单          
            usleep(60000000);
            $i++;
        } while (true);
    } */
		//开始自动程序
		public function run(){
			//获取上线货品
			$sql="select * from yang_currency where is_line = 1";
			$currency_list=$this->select($sql);
			//$sql="update yang_member set rmb =1000";
			//$this->query($sql);
			 foreach ($currency_list as $k =>$v){
				 $this->source($v['currency_url'],$v['currency_id'],$v['lever']);
			 }
		}
		private function source($code,$currency_id,$lever){			
			//接口url			
			$url1m='http://kxian.ronmei.com/chart/chart.php?code='.$code.'&rows=200&interval=1';
			$result1m=$this->aaa($url1m,$lever);
			//存入文件位置
			$filename1m = "/home/wwwroot/wp/KlineData/Minute2_".$currency_id.".js";
			file_put_contents($filename1m, $result1m);			
    	}
				
	
	private function aaa($url,$lever){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$result = curl_exec($ch);
		curl_close ($ch);
		$newresult=json_decode($result);
		foreach ($newresult as $k=>$v){
			$ran=$this->random();
			$newarray[$k][]=str_replace('"','',(substr($v[0],0,strlen($v[0])-3)));
			$newarray[$k][]=$v[4]*$lever+$ran;
		}
		$resultfinal=json_encode($newarray);
		return $resultfinal;
	}
	private function random(){
		$time=time();
		$timen=substr($time, -2);
		$minute=date('i',$time);
		$second=date('s',$time);
		$num=round(($timen+$minute+$second)/300,2);
		if($timen%2==0){
			$num=0-$num;
		}
		return $num;
	}
	}
	$auto=new auto();
	$auto->run();
	
?>